<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/1/12 22:10
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

use Wechat\Loader;
use Wechat\WechatReceive;

class Notify extends WX_Controller
{

    /**
     * @var \Wechat\WechatReceive
     */
    private $wechat = null;

    /**
     * 初始化
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 微信主动通知接口
     */
    public function index()
    {
        Loader::config($this->options);
        $this->wechat = Loader::get('receive');
        if (!$this->wechat->valid()) {
            return false;
        }

        $type = $this->wechat->getRev()->getRevType();
        $fromer = $this->wechat->getRevFrom();
        $text = !empty($this->setting['welcome']) ? $this->setting['welcome'] : '欢迎关注';

        switch($type) {
            case WechatReceive::MSGTYPE_EVENT:
                $this->_event($fromer);
                $this->wechat->text($text)->reply();
                break;
            case WechatReceive::MSGTYPE_TEXT:
                $this->wechat->text($text)->reply();
                break;
            default:
                $this->wechat->text($text)->reply();
                break;
        }
    }

    /**
     * 事件类型
     * @param string $openid
     */
    public function _event($openid = '')
    {
        $event = $this->wechat->getRevEvent();
        $type = $event['event'];
        $wechatUser = Loader::get('user');
        $user = $wechatUser->getUserInfo($openid);
        $sceneId = $this->wechat->getRevSceneId();

        switch($type) {
            case "subscribe":
                if (!empty($sceneId)) {
                    $this->_doSceneCase($openid, $sceneId, $user);
                } else {
                    $this->_saveOrUpdate($user);
                }
                break;
            case "unsubscribe":
                $this->_saveOrUpdate($user);
                break;
            case "SCAN":
                if (!empty($sceneId)) {
                    $this->_doSceneCase($openid, $sceneId, $user);
                } else {
                    $this->_saveOrUpdate($user);
                }
                break;
            case "CLICK":
                switch ($event['key']) {
                    case "SERVICE":
                        $this->_doService();
                        break;
                    case "CARD":
                        $this->_doCard();
                        break;
                }
                break;
        }
    }

    /**
     * 储蓄卡
     */
    function _doCard()
    {
        $text = '即将推出，敬请期待~';
        $this->wechat->text($text)->reply();
        exit;
    }

    /**
     * 联系我们
     */
    function _doService()
    {
        $text = '[太阳]丘丘谷健康生活方式！'
                . "\r\n"
                . '欢迎关注丘丘谷商城！'
                . "\r\n"
                . "\r\n"
                . '1、您可以在此直接输入问题，微信客服为您服务！'
                . "\r\n"
                . '2、您也可以直接拨打丘丘谷客服热线：400-873-0757'
                . "\r\n"
                . '3、商务合作：13661919391王先生'
                . "\r\n"
                . "\r\n"
                . "\r\n"
                . '[月亮]客服工作时间：'
                . "\r\n"
                . '周一~周五，每天9:00~17:00 非工作时间留言，我们会及时回复！谢谢！';

        $this->wechat->text($text)->reply();
        exit;
    }

    /**
     * 处理带场景的扫描
     * @param $openid
     * @param $sceneId
     * @param $user
     * @return bool
     */
    public function _doSceneCase($openid, $sceneId, $user)
    {
        $this->load->model('wx/user/User_Model', 'user');
        $row = $this->user->getQrcodeParmas($sceneId);
        if (empty($row)) {
            return false;
        }

        $params = json_decode($row['value'], true);
        $this->$row['scene']($openid, $params, $user);
    }

    /**
     * 分销建立对应关系
     * @param $params
     * @param $user
     */
    private function fenxiao($openid, $params, $user)
    {
        $this->load->model('wx/user/Social_Model', 'social');
        $userinfo = $this->social->getUserByOpenId($openid);
        if (empty($userinfo)) {
            return false;
        }
        $userinfo = $this->_saveOrUpdate($user, true);
        $userid = $userinfo['userid'];
        $puserid = $params['puserid'];
        if ($userid == $puserid) {
            return false;
        }
        $ppuserid = 0;
        $this->load->model('wx/user/SocialRelationShip_Model', 'relation');
        $row = $this->relation->getRelationItem($puserid);
        if (!empty($row)) {
            $ppuserid = $row['puserid'];
        }
        $this->relation->setRelationItem($userid, $puserid, $ppuserid);
    }

    /**
     * @param $user
     */
    private function _saveOrUpdate($user = array(), $return = false)
    {
        $this->load->model('wx/user/Social_Model', 'social');
        return $this->social->saveUpdate($user, $return);
    }
}